package com.cafuc.base;

import com.cafuc.entity.User;
import com.cafuc.service.UserService;
import com.cafuc.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyOperation {
    public void proxyTest(){
        //一般写法
        UserService us = new UserServiceImpl();
        us.insertUser(new User());

        //动态代理
        UserService us1 = (UserService) Proxy.newProxyInstance(
                UserService.class.getClassLoader(),
                new Class[]{UserService.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("222222222");
                        return null;
                    }
                });
        us1.insertUser(new User());
    }
    public static void main(String[] args){
        ProxyOperation po = new ProxyOperation();
        po.proxyTest();
    }
}
